<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Android Development with OpenCV</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../df/d65/tutorial_table_of_content_introduction.html">Introduction to OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Android Development with OpenCV </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../da/d2a/tutorial_O4A_SDK.html">OpenCV4Android SDK</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d7/dbd/tutorial_android_ocl_intro.html">Use OpenCL in Android camera preview based CV application</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Vsevolod Glumov </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<dl class="section warning"><dt>Warning</dt><dd>This tutorial is deprecated.</dd></dl>
<p>This tutorial has been created to help you use OpenCV library within your Android project.</p>
<p>This guide was written with Windows 7 in mind, though it should work with any other OS supported by OpenCV4Android SDK.</p>
<p>This tutorial assumes you have the following installed and configured:</p>
<ul>
<li>JDK</li>
<li>Android SDK and NDK</li>
<li>Eclipse IDE</li>
<li>ADT and CDT plugins for Eclipse</li>
</ul>
<p>If you need help with anything of the above, you may refer to our <a class="el" href="../../d9/d3f/tutorial_android_dev_intro.html">Introduction into Android Development</a> guide.</p>
<p>This tutorial also assumes you have OpenCV4Android SDK already installed on your development machine and OpenCV Manager on your testing device correspondingly. If you need help with any of these, you may consult our <a class="el" href="../../da/d2a/tutorial_O4A_SDK.html">OpenCV4Android SDK</a> tutorial.</p>
<p>If you encounter any error after thoroughly following these steps, feel free to contact us via <a href="https://groups.google.com/group/android-opencv/">OpenCV4Android</a> discussion group or OpenCV <a href="https://forum.opencv.org">Q&amp;A forum</a> . We'll do our best to help you out.</p>
<h2>Using OpenCV Library Within Your Android Project </h2>
<p>In this section we will explain how to make some existing project to use OpenCV. Starting with 2.4.2 release for Android, <em>OpenCV Manager</em> is used to provide apps with the best available version of OpenCV. You can get more information here: <code>Android OpenCV Manager</code> and in these <a href="https://docs.google.com/a/itseez.com/presentation/d/1EO_1kijgBg_BsjNp2ymk-aarg-0K279_1VZRcPplSuk/present#slide=id.p">slides</a>.</p>
<h3>Java</h3>
<h4>Application Development with Async Initialization</h4>
<p>Using async initialization is a <b>recommended</b> way for application development. It uses the OpenCV Manager to access OpenCV libraries externally installed in the target system.</p>
<ol type="1">
<li><p class="startli">Add OpenCV library project to your workspace. Use menu File -&gt; Import -&gt; Existing project in your workspace.</p>
<p class="startli">Press Browse button and locate OpenCV4Android SDK (<code>OpenCV-2.4.9-android-sdk/sdk</code>).</p>
<div class="image">
<img src="../../eclipse_opencv_dependency0.png" alt="eclipse_opencv_dependency0.png"/>
</div>
</li>
<li><p class="startli">In application project add a reference to the OpenCV Java SDK in Project -&gt; Properties -&gt; Android -&gt; Library -&gt; Add select OpenCV Library - 2.4.9.</p>
<div class="image">
<img src="../../eclipse_opencv_dependency1.png" alt="eclipse_opencv_dependency1.png"/>
</div>
</li>
</ol>
<p>In most cases OpenCV Manager may be installed automatically from Google Play. For the case, when Google Play is not available, i.e. emulator, developer board, etc, you can install it manually using adb tool. See <code>Manager Selection</code> for details.</p>
<p>There is a very base code snippet implementing the async initialization. It shows basic principles. See the "15-puzzle" OpenCV sample for details. </p><div class="fragment"><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>Sample1Java <span class="keyword">extends</span> Activity implements CvCameraViewListener {</div><div class="line"></div><div class="line">    <span class="keyword">private</span> BaseLoaderCallback mLoaderCallback = <span class="keyword">new</span> BaseLoaderCallback(<span class="keyword">this</span>) {</div><div class="line">        @Override</div><div class="line">        <span class="keyword">public</span> <span class="keywordtype">void</span> onManagerConnected(<span class="keywordtype">int</span> status) {</div><div class="line">            <span class="keywordflow">switch</span> (status) {</div><div class="line">                <span class="keywordflow">case</span> LoaderCallbackInterface.SUCCESS:</div><div class="line">                {</div><div class="line">                    Log.i(TAG, <span class="stringliteral">&quot;OpenCV loaded successfully&quot;</span>);</div><div class="line">                    mOpenCvCameraView.enableView();</div><div class="line">                } <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">default</span>:</div><div class="line">                {</div><div class="line">                    super.onManagerConnected(status);</div><div class="line">                } <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">        }</div><div class="line">    };</div><div class="line"></div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> onResume()</div><div class="line">    {</div><div class="line">        super.onResume();</div><div class="line">        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, <span class="keyword">this</span>, mLoaderCallback);</div><div class="line">    }</div><div class="line"></div><div class="line">    ...</div><div class="line">}</div></div><!-- fragment --><p> It this case application works with OpenCV Manager in asynchronous fashion. OnManagerConnected callback will be called in UI thread, when initialization finishes. Please note, that it is not allowed to use OpenCV calls or load OpenCV-dependent native libs before invoking this callback. Load your own native libraries that depend on OpenCV after the successful OpenCV initialization. Default BaseLoaderCallback implementation treat application context as Activity and calls <a class="el" href="../../dc/d83/group__core__opencl.html#gabfca32247f267c662b3bff6587d14b40">Activity.finish()</a> method to exit in case of initialization failure. To override this behavior you need to override <a class="el" href="../../dc/d83/group__core__opencl.html#gabfca32247f267c662b3bff6587d14b40">finish()</a> method of BaseLoaderCallback class and implement your own finalization method.</p>
<h4>Application Development with Static Initialization</h4>
<p>According to this approach all OpenCV binaries are included into your application package. It is designed mostly for development purposes. This approach is deprecated for the production code, release package is recommended to communicate with OpenCV Manager via the async initialization described above.</p>
<ol type="1">
<li><p class="startli">Add the OpenCV library project to your workspace the same way as for the async initialization above. Use menu File -&gt; Import -&gt; Existing project in your workspace, press Browse button and select OpenCV SDK path (<code>OpenCV-2.4.9-android-sdk/sdk</code>).</p>
<div class="image">
<img src="../../eclipse_opencv_dependency0.png" alt="eclipse_opencv_dependency0.png"/>
</div>
</li>
<li><p class="startli">In the application project add a reference to the OpenCV4Android SDK in Project -&gt; Properties -&gt; Android -&gt; Library -&gt; Add select OpenCV Library - 2.4.9;</p>
<div class="image">
<img src="../../eclipse_opencv_dependency1.png" alt="eclipse_opencv_dependency1.png"/>
</div>
</li>
<li><p class="startli">If your application project <b>doesn't have a JNI part</b>, just copy the corresponding OpenCV native libs from <code>&lt;OpenCV-2.4.9-android-sdk&gt;/sdk/native/libs/&lt;target_arch&gt;</code> to your project directory to folder <code>libs/&lt;target_arch&gt;</code>.</p>
<p class="startli">In case of the application project <b>with a JNI part</b>, instead of manual libraries copying you need to modify your Android.mk file: add the following two code lines after the "include $(CLEAR_VARS)" and before "include path_to_OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk" </p><div class="fragment"><div class="line">OPENCV_CAMERA_MODULES:=on</div><div class="line">OPENCV_INSTALL_MODULES:=on</div></div><!-- fragment --><p> The result should look like the following: </p><div class="fragment"><div class="line">include $(CLEAR_VARS)</div><div class="line"></div><div class="line"># OpenCV</div><div class="line">OPENCV_CAMERA_MODULES:=on</div><div class="line">OPENCV_INSTALL_MODULES:=on</div><div class="line">include ../../sdk/native/jni/OpenCV.mk</div></div><!-- fragment --><p> After that the OpenCV libraries will be copied to your application <code>libs</code> folder during the JNI build.v</p>
<p class="startli">Eclipse will automatically include all the libraries from the <code>libs</code> folder to the application package (APK).</p>
</li>
<li>The last step of enabling OpenCV in your application is Java initialization code before calling OpenCV API. It can be done, for example, in the static section of the Activity class: <div class="fragment"><div class="line"><span class="keyword">static</span> {</div><div class="line">    <span class="keywordflow">if</span> (!OpenCVLoader.initDebug()) {</div><div class="line">        <span class="comment">// Handle initialization error</span></div><div class="line">    }</div><div class="line">}</div></div><!-- fragment --> If you application includes other OpenCV-dependent native libraries you should load them <b>after</b> OpenCV initialization: <div class="fragment"><div class="line"><span class="keyword">static</span> {</div><div class="line">    <span class="keywordflow">if</span> (!OpenCVLoader.initDebug()) {</div><div class="line">        <span class="comment">// Handle initialization error</span></div><div class="line">    } <span class="keywordflow">else</span> {</div><div class="line">        System.loadLibrary(<span class="stringliteral">&quot;my_jni_lib1&quot;</span>);</div><div class="line">        System.loadLibrary(<span class="stringliteral">&quot;my_jni_lib2&quot;</span>);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment --></li>
</ol>
<h3>Native/C++</h3>
<p>To build your own Android application, using OpenCV as native part, the following steps should be taken:</p>
<ol type="1">
<li>You can use an environment variable to specify the location of OpenCV package or just hardcode absolute or relative path in the <code>jni/Android.mk</code> of your projects.</li>
<li><p class="startli">The file <code>jni/Android.mk</code> should be written for the current application using the common rules for this file.</p>
<p class="startli">For detailed information see the Android NDK documentation from the Android NDK archive, in the file <code>&lt;path_where_NDK_is_placed&gt;/docs/ANDROID-MK.html</code>.</p>
</li>
<li>The following line: <div class="fragment"><div class="line">include C:\Work\OpenCV4Android\OpenCV-2.4.9-android-sdk\sdk\native\jni\OpenCV.mk</div></div><!-- fragment --> Should be inserted into the <code>jni/Android.mk</code> file <b>after</b> this line: <div class="fragment"><div class="line">include $(CLEAR_VARS)</div></div><!-- fragment --></li>
<li><p class="startli">Several variables can be used to customize OpenCV stuff, but you <b>don't need</b> to use them when your application uses the async initialization via the OpenCV Manager API.</p>
<dl class="section note"><dt>Note</dt><dd>These variables should be set <b>before</b> the "include .../OpenCV.mk" line: <div class="fragment"><div class="line">OPENCV_INSTALL_MODULES:=on</div></div><!-- fragment --></dd></dl>
<p>Copies necessary OpenCV dynamic libs to the project libs folder in order to include them into the APK. </p><div class="fragment"><div class="line">OPENCV_CAMERA_MODULES:=off</div></div><!-- fragment --><p> Skip native OpenCV camera related libs copying to the project libs folder. </p><div class="fragment"><div class="line">OPENCV_LIB_TYPE:=STATIC</div></div><!-- fragment --><p> Perform static linking with OpenCV. By default dynamic link is used and the project JNI lib depends on libopencv_java.so.</p>
</li>
<li><p class="startli">The file <code>Application.mk</code> should exist and should contain lines: </p><div class="fragment"><div class="line">APP_STL := gnustl_static</div><div class="line">APP_CPPFLAGS := -frtti -fexceptions</div></div><!-- fragment --><p> Also, the line like this one: </p><div class="fragment"><div class="line">APP_ABI := armeabi-v7a</div></div><!-- fragment --><p> Should specify the application target platforms.</p>
<p class="startli">In some cases a linkage error (like `"In function 'cv::toUtf16(std::basic_string&lt;...&gt;... undefined reference to 'mbstowcs'"`) happens when building an application JNI library, depending on OpenCV. The following line in the <code>Application.mk</code> usually fixes it: </p><div class="fragment"><div class="line">APP_PLATFORM := android-9</div></div><!-- fragment --></li>
<li>Either use <a class="el" href="../../d9/d3f/tutorial_android_dev_intro.html#tutorial_android_dev_intro_ndk">manual</a> ndk-build invocation or <a class="el" href="../../d9/d3f/tutorial_android_dev_intro.html#tutorial_android_dev_intro_eclipse">setup Eclipse CDT Builder</a> to build native JNI lib before (re)building the Java part and creating an APK.</li>
</ol>
<h2>Hello OpenCV Sample </h2>
<p>Here are basic steps to guide you trough the process of creating a simple OpenCV-centric application. It will be capable of accessing camera output, processing it and displaying the result.</p>
<ol type="1">
<li>Open Eclipse IDE, create a new clean workspace, create a new Android project File &ndash;&gt; New &ndash;&gt; Android Project</li>
<li>Set name, target, package and minSDKVersion accordingly. The minimal SDK version for build with OpenCV4Android SDK is 11. Minimal device API Level (for application manifest) is 8.</li>
<li>Allow Eclipse to create default activity. Lets name the activity HelloOpenCvActivity.</li>
<li>Choose Blank Activity with full screen layout. Lets name the layout HelloOpenCvLayout.</li>
<li>Import OpenCV library project to your workspace.</li>
<li><p class="startli">Reference OpenCV library within your project properties.</p>
<div class="image">
<img src="../../dev_OCV_reference.png" alt="dev_OCV_reference.png"/>
</div>
</li>
<li>Edit your layout file as xml file and pass the following layout there: <div class="fragment"><div class="line">&lt;<span class="keywordtype">LinearLayout</span> <span class="keyword">xmlns:android</span>=<span class="stringliteral">&quot;http://schemas.android.com/apk/res/android&quot;</span></div><div class="line">    <span class="keyword">xmlns:tools</span>=<span class="stringliteral">&quot;http://schemas.android.com/tools&quot;</span></div><div class="line">    <span class="keyword">xmlns:opencv</span>=<span class="stringliteral">&quot;http://schemas.android.com/apk/res-auto&quot;</span></div><div class="line">    <span class="keyword">android:layout_width</span>=<span class="stringliteral">&quot;match_parent&quot;</span></div><div class="line">    <span class="keyword">android:layout_height</span>=<span class="stringliteral">&quot;match_parent&quot;</span> &gt;</div><div class="line"></div><div class="line">    &lt;<span class="keywordtype">org.opencv.android.JavaCameraView</span></div><div class="line">        <span class="keyword">android:layout_width</span>=<span class="stringliteral">&quot;fill_parent&quot;</span></div><div class="line">        <span class="keyword">android:layout_height</span>=<span class="stringliteral">&quot;fill_parent&quot;</span></div><div class="line">        <span class="keyword">android:visibility</span>=<span class="stringliteral">&quot;gone&quot;</span></div><div class="line">        <span class="keyword">android:id</span>=<span class="stringliteral">&quot;@+id/HelloOpenCvView&quot;</span></div><div class="line">        <span class="keyword">opencv:show_fps</span>=<span class="stringliteral">&quot;true&quot;</span></div><div class="line">        <span class="keyword">opencv:camera_id</span>=<span class="stringliteral">&quot;any&quot;</span> /&gt;</div><div class="line"></div><div class="line">&lt;/<span class="keywordtype">LinearLayout</span>&gt;</div></div><!-- fragment --></li>
<li>Add the following permissions to the <code>AndroidManifest.xml</code> file: <div class="fragment"><div class="line">&lt;/<span class="keywordtype">application</span>&gt;</div><div class="line"></div><div class="line">&lt;<span class="keywordtype">uses-permission</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.permission.CAMERA&quot;</span>/&gt;</div><div class="line"></div><div class="line">&lt;<span class="keywordtype">uses-feature</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.hardware.camera&quot;</span> <span class="keyword">android:required</span>=<span class="stringliteral">&quot;false&quot;</span>/&gt;</div><div class="line">&lt;<span class="keywordtype">uses-feature</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.hardware.camera.autofocus&quot;</span> <span class="keyword">android:required</span>=<span class="stringliteral">&quot;false&quot;</span>/&gt;</div><div class="line">&lt;<span class="keywordtype">uses-feature</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.hardware.camera.front&quot;</span> <span class="keyword">android:required</span>=<span class="stringliteral">&quot;false&quot;</span>/&gt;</div><div class="line">&lt;<span class="keywordtype">uses-feature</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.hardware.camera.front.autofocus&quot;</span> <span class="keyword">android:required</span>=<span class="stringliteral">&quot;false&quot;</span>/&gt;</div></div><!-- fragment --></li>
<li>Set application theme in AndroidManifest.xml to hide title and system buttons. <div class="fragment"><div class="line">&lt;<span class="keywordtype">application</span></div><div class="line">    <span class="keyword">android:icon</span>=<span class="stringliteral">&quot;@drawable/icon&quot;</span></div><div class="line">    <span class="keyword">android:label</span>=<span class="stringliteral">&quot;@string/app_name&quot;</span></div><div class="line">    <span class="keyword">android:theme</span>=<span class="stringliteral">&quot;@android:style/Theme.NoTitleBar.Fullscreen&quot;</span> &gt;</div></div><!-- fragment --></li>
<li>Add OpenCV library initialization to your activity. Fix errors by adding required imports. <div class="fragment"><div class="line"><span class="keyword">private</span> BaseLoaderCallback mLoaderCallback = <span class="keyword">new</span> BaseLoaderCallback(<span class="keyword">this</span>) {</div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> onManagerConnected(<span class="keywordtype">int</span> status) {</div><div class="line">        <span class="keywordflow">switch</span> (status) {</div><div class="line">            <span class="keywordflow">case</span> LoaderCallbackInterface.SUCCESS:</div><div class="line">            {</div><div class="line">                Log.i(TAG, <span class="stringliteral">&quot;OpenCV loaded successfully&quot;</span>);</div><div class="line">                mOpenCvCameraView.enableView();</div><div class="line">            } <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">default</span>:</div><div class="line">            {</div><div class="line">                super.onManagerConnected(status);</div><div class="line">            } <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">    }</div><div class="line">};</div><div class="line"></div><div class="line">@Override</div><div class="line"><span class="keyword">public</span> <span class="keywordtype">void</span> onResume()</div><div class="line">{</div><div class="line">    super.onResume();</div><div class="line">    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, <span class="keyword">this</span>, mLoaderCallback);</div><div class="line">}</div></div><!-- fragment --></li>
<li>Defines that your activity implements CvCameraViewListener2 interface and fix activity related errors by defining missed methods. For this activity define onCreate, onDestroy and onPause and implement them according to the code snippet below. Fix errors by adding required imports. <div class="fragment"><div class="line"><span class="keyword">private</span> CameraBridgeViewBase mOpenCvCameraView;</div><div class="line"></div><div class="line">@Override</div><div class="line"><span class="keyword">public</span> <span class="keywordtype">void</span> onCreate(Bundle savedInstanceState) {</div><div class="line">    Log.i(TAG, <span class="stringliteral">&quot;called onCreate&quot;</span>);</div><div class="line">    super.onCreate(savedInstanceState);</div><div class="line">    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);</div><div class="line">    setContentView(R.layout.HelloOpenCvLayout);</div><div class="line">    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);</div><div class="line">    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);</div><div class="line">    mOpenCvCameraView.setCvCameraViewListener(<span class="keyword">this</span>);</div><div class="line">}</div><div class="line"></div><div class="line">@Override</div><div class="line"><span class="keyword">public</span> <span class="keywordtype">void</span> onPause()</div><div class="line">{</div><div class="line">    super.onPause();</div><div class="line">    <span class="keywordflow">if</span> (mOpenCvCameraView != null)</div><div class="line">        mOpenCvCameraView.disableView();</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keywordtype">void</span> onDestroy() {</div><div class="line">    super.onDestroy();</div><div class="line">    <span class="keywordflow">if</span> (mOpenCvCameraView != null)</div><div class="line">        mOpenCvCameraView.disableView();</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keywordtype">void</span> onCameraViewStarted(<span class="keywordtype">int</span> width, <span class="keywordtype">int</span> height) {</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keywordtype">void</span> onCameraViewStopped() {</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> Mat onCameraFrame(CvCameraViewFrame inputFrame) {</div><div class="line">    <span class="keywordflow">return</span> inputFrame.rgba();</div><div class="line">}</div></div><!-- fragment --></li>
<li>Run your application on device or emulator.</li>
</ol>
<p>Lets discuss some most important steps. Every Android application with UI must implement Activity and View. By the first steps we create blank activity and default view layout. The simplest OpenCV-centric application must implement OpenCV initialization, create its own view to show preview from camera and implements CvCameraViewListener2 interface to get frames from camera and process it.</p>
<p>First of all we create our application view using xml layout. Our layout consists of the only one full screen component of class org.opencv.android.JavaCameraView. This class is implemented inside OpenCV library. It is inherited from CameraBridgeViewBase, that extends SurfaceView and uses standard Android camera API.</p>
<p>After creating layout we need to implement Activity class. OpenCV initialization process has been already discussed above. In this sample we use asynchronous initialization. Implementation of CvCameraViewListener interface allows you to add processing steps after frame grabbing from camera and before its rendering on screen. The most important function is onCameraFrame. It is callback function and it is called on retrieving frame from camera. The callback input is object of CvCameraViewFrame class that represents frame from camera.</p>
<dl class="section note"><dt>Note</dt><dd>Do not save or use CvCameraViewFrame object out of onCameraFrame callback. This object does not have its own state and its behavior out of callback is unpredictable!</dd></dl>
<p>It has rgba() and gray() methods that allows to get frame as RGBA and one channel gray scale Mat respectively. It expects that onCameraFrame function returns RGBA frame that will be drawn on the screen. </p>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:36 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
